package com.tengju.data.infrastructure.service.convert;

import com.tengju.data.domain.model.report.AchievementBonusDateConfig;
import com.tengju.data.domain.model.report.DataAchievementBonusDateConfigType;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.DataAchievementBonusDateConfigDO;
import com.tengju.data.infrastructure.shared.InfraException;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author chenhang
 * @Date 2021/9/26
 */
public class AchievementBonusDateConfigConvert {

    public static AchievementBonusDateConfig convertToModel(List<DataAchievementBonusDateConfigDO> configList) {
        if (CollectionUtils.isEmpty(configList)) {
            return null;
        }
        LocalDate startDate = null;
        LocalDate endDate = null;
        String dataKey = null;
        LocalDate lastCalDate = null;
        LocalDate settleDate = null;
        LocalDate riskDate = null;
        for (DataAchievementBonusDateConfigDO dataAchievementBonusDateConfigDO : configList) {
            Integer configTypeVal = dataAchievementBonusDateConfigDO.getConfigType();
            DataAchievementBonusDateConfigType configType = DataAchievementBonusDateConfigType.parse(configTypeVal);
            dataKey = dataAchievementBonusDateConfigDO.getDataKey();
            switch (configType) {
                case SETTLE_DATE:
                    if (settleDate != null) {
                        throw new InfraException("补贴配置转换异常" + dataKey);
                    }
                    settleDate = dataAchievementBonusDateConfigDO.getStartTime();
                    break;
                case DATA_RANGE:
                    if (startDate != null || endDate != null) {
                        throw new InfraException("补贴配置转换异常" + dataKey);
                    }
                    startDate = dataAchievementBonusDateConfigDO.getStartTime();
                    endDate = dataAchievementBonusDateConfigDO.getEndTime();
                    break;
                case LAST_CAL_DATE:
                    if (lastCalDate != null) {
                        throw new InfraException("补贴配置转换异常" + dataKey);
                    }
                    lastCalDate = dataAchievementBonusDateConfigDO.getStartTime();
                    break;
                case CAL_RISK:
                    if (riskDate != null) {
                        throw new InfraException("补贴配置转换异常" + dataKey);
                    }
                    riskDate = dataAchievementBonusDateConfigDO.getStartTime();
                    break;
            }
        }
        return new AchievementBonusDateConfig(dataKey,startDate,endDate,lastCalDate,settleDate,riskDate);
    }

    public static List<DataAchievementBonusDateConfigDO> convertToDOList(AchievementBonusDateConfig dateConfig) {
        if (dateConfig == null) {
            return Collections.emptyList();
        }
        List<DataAchievementBonusDateConfigDO> result = new ArrayList<>();
        if (dateConfig.getStartDate() != null  && dateConfig.getEndDate() != null) {
            DataAchievementBonusDateConfigDO dateRange = new DataAchievementBonusDateConfigDO();
            dateRange.setDataKey(dateConfig.getDataKey());
            dateRange.setConfigType(DataAchievementBonusDateConfigType.DATA_RANGE.getCode());
            dateRange.setStartTime(dateConfig.getStartDate());
            dateRange.setEndTime(dateConfig.getEndDate());
            result.add(dateRange);
        }
        if (dateConfig.getSettleDate() != null) {
            DataAchievementBonusDateConfigDO dateRange = new DataAchievementBonusDateConfigDO();
            dateRange.setDataKey(dateConfig.getDataKey());
            dateRange.setConfigType(DataAchievementBonusDateConfigType.SETTLE_DATE.getCode());
            dateRange.setStartTime(dateConfig.getSettleDate());
            dateRange.setEndTime(dateConfig.getSettleDate());
            result.add(dateRange);
        }
        if (dateConfig.getLastCalDate() != null) {
            DataAchievementBonusDateConfigDO dateRange = new DataAchievementBonusDateConfigDO();
            dateRange.setDataKey(dateConfig.getDataKey());
            dateRange.setConfigType(DataAchievementBonusDateConfigType.LAST_CAL_DATE.getCode());
            dateRange.setStartTime(dateConfig.getLastCalDate());
            dateRange.setEndTime(dateConfig.getLastCalDate());
            result.add(dateRange);
        }
        if (dateConfig.getRiskDate() != null) {
            DataAchievementBonusDateConfigDO dateRange = new DataAchievementBonusDateConfigDO();
            dateRange.setDataKey(dateConfig.getDataKey());
            dateRange.setConfigType(DataAchievementBonusDateConfigType.CAL_RISK.getCode());
            dateRange.setStartTime(dateConfig.getRiskDate());
            dateRange.setEndTime(dateConfig.getRiskDate());
            result.add(dateRange);
        }
        return result;
    }
}
